import { EnumApprovalNewStatus } from '#/config/enum/Biz'
import { $t } from '@vben/locales'
import type { Recordable } from '@vben/types'
import type { AnyFn } from '@vueuse/core'
import { Input, message } from 'ant-design-vue'
import { ref } from 'vue'
import { useMessage } from './useMessage'

/**
 * 审核按钮
 */
export function useApprovalBtn(opts: {
    getSelectRows: <T = Recordable<any>>() => T[]
    idKey?: string
    onSubmit: (params: {
        /**
         * 审批意见或备注
         */
        approvalComment?: string
        /**
         * 审批状态 1.通过 2.拒绝
         */
        approvalStatus: number
        /**
         * 业务id列表
         */
        businessIdList: number[]
    }) => Promise<any>
    /**
     * 拒绝理由最小长度
     */
    rejectMinLength?: number

    reload?: AnyFn
    statusKey?: string
    /**
     * 审核拒绝状态
     */
    statusReject?: number
    /**
     * 审核成功状态 0. 审核中 1.审批通过 2.审批拒绝
     */
    statusSuccess?: number
    /**
     * 审核中状态
     */
    statusWaiting?: number
}) {
    const {
        getSelectRows,
        statusSuccess = EnumApprovalNewStatus.APPROVED,
        statusWaiting = EnumApprovalNewStatus.WAITING,
        statusReject = EnumApprovalNewStatus.REJECTED,
        statusKey = 'approvalStatus',
        idKey = 'id',
        onSubmit,
        reload,
        rejectMinLength = 5
    } = opts

    function isStatusReject(status: number | string) {
        return status === statusReject
    }

    function isStatusSuccess(status: number | string) {
        return status === statusSuccess
    }

    function isStatusWaiting(status: number | string) {
        return status === statusWaiting
    }

    // 审核
    function onApprovalClick(status: number, rows?: Array<Recordable<any>>) {
        if (!rows) {
            rows = getSelectRows()
        }

        if (rows.length === 0)
            return message.warning($t('hook.useApprovalBtn.approval.minSelectTip'))
        const bool = rows.some((e) => e[statusKey] !== statusWaiting)

        if (bool) {
            return message.error($t('hook.useApprovalBtn.approval.onlyWaitingTip'))
        }

        const { createConfirm } = useMessage()

        if (status === statusSuccess) {
            createConfirm({
                title: $t('hook.useApprovalBtn.approval.confirmTitle'),
                iconType: 'warning',
                content: $t('hook.useApprovalBtn.approval.confirmContent'),
                onOk: async () => {
                    onApprovalSubmit(status, rows)
                }
            })
        } else {
            onApprovalSubmit(status, rows)
        }
    }

    async function onApprovalSubmit(status: number, rows?: Array<Recordable<any>>) {
        const businessIdList = (rows || getSelectRows()).map((e) => e[idKey])
        if (isStatusReject(status)) {
            const { createConfirm } = useMessage()
            const approvalComment = ref<string>('')
            const maxLengthTip = $t('hook.useApprovalBtn.approval.rejectMinLengthTip', [
                rejectMinLength
            ])

            createConfirm({
                iconType: 'info',
                title: $t('hook.useApprovalBtn.approval.rejectTitle'),
                content: (
                    <div class="p-2">
                        <Input.TextArea
                            onInput={(e: any) => {
                                const value = e.target.value
                                approvalComment.value = value
                            }}
                            placeholder={maxLengthTip}
                            rows={4}
                        />
                    </div>
                ),
                async onOk() {
                    if (approvalComment.value?.length < rejectMinLength) {
                        message.error(maxLengthTip)

                        throw maxLengthTip
                    }

                    if (onSubmit) {
                        try {
                            await onSubmit({
                                businessIdList,
                                approvalStatus: status,
                                approvalComment: approvalComment.value
                            })
                            if (reload) {
                                reload()
                            }
                        } catch (error) {
                            console.log(error)
                        }
                    }
                }
            })
        } else {
            if (onSubmit) {
                try {
                    await onSubmit({
                        businessIdList,
                        approvalStatus: status
                    })
                    if (reload) {
                        reload()
                    }
                } catch (error) {
                    console.log(error)
                }
            }
        }
    }

    return {
        onApprovalClick,
        isStatusReject,
        isStatusSuccess,
        isStatusWaiting,
        onApprovalSubmit
    }
}
